/*
 * Filename:    CalculationFormula.java
 * Copyright:   2014 中国平安保险（集团）股份有限公司 版权所有
 * Company:     平安科技养老险及健康险系统开发部
 * Create at:   2014年10月13日
 */
package com.paic.mhis.hcpms.quota.calculationreport.calculationkpi.common.calculation;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.paic.mhis.hcpms.quota.calculationreport.calculationkpi.common.util.CalculationMathValueUtil;
import com.paic.mhis.hcpms.quota.calculationreport.calculationkpi.common.util.ContentFinalUtil;
import com.paic.mhis.hcpms.quota.calculationreport.calculationkpi.common.vo.CalculationFormulaVo;
import com.paic.mhis.hcpms.quota.calculationreport.calculationkpi.common.vo.CalculationResultVo;
import com.paic.mhis.hcpms.quota.calculationreport.calculationkpi.common.vo.FormulaItemVo;
import com.paic.mhis.hcpms.quota.calculationreport.calculationkpi.common.vo.FormulaLumpVo;
import com.paic.mhis.hcpms.quota.calculationreport.calculationkpi.common.vo.ItemPropertiesVo;

public class CalculationFormula {
	private CalculationFormulaVo calculationFormulaVo;
	private Map<String, Object> propertiesMap = new HashMap<String, Object>();
	private Map<String, BigDecimal> LumpValueMap = new HashMap<String, BigDecimal>();
	private CalculationResultVo calculationResultVo;

	public void addFormula(CalculationFormulaVo vo, Map<String, Object> map) {
		setCalculationFormulaVo(vo);
		setPropertiesMap(map);
	}

	public void calculation() {
		setResultModel();
		List<FormulaLumpVo> lumpList = calculationFormulaVo.getLumpList();
		for (int i = 0; i < lumpList.size(); i++) {
			FormulaLumpVo lumpVo = lumpList.get(i);
			calculationFormulaLump(lumpVo);
		}
	}

	private void setResultModel() {
		calculationResultVo = new CalculationResultVo();
		calculationResultVo.setExpressionId(calculationFormulaVo.getExpressionId());
		calculationResultVo.setFormula(calculationFormulaVo.getFormula());
		calculationFormulaVo.setKpiCode(calculationFormulaVo.getKpiCode());
	}

	private void calculationFormulaLump(FormulaLumpVo lumpVo) {
		List<FormulaItemVo> itemList = lumpVo.getItemList();
		BigDecimal leftValue = new BigDecimal(0);
		BigDecimal rightValue = new BigDecimal(0);
		BigDecimal resultValue = new BigDecimal(0);
		Map<String, BigDecimal> itemValueMap = new HashMap<String, BigDecimal>();
		for (int j = 0; j < itemList.size(); j++) {
			FormulaItemVo itemVo = itemList.get(j);
			leftValue = getProperValue(itemVo.getProperLeftType(), itemVo.getProperLeftName(),
					itemVo.getProperLeftValue(), itemValueMap);
			if (!"".equals(itemVo.getProperRightName()) || itemVo.getProperRightName() != null) {
				rightValue = getProperValue(itemVo.getProperRightType(), itemVo.getProperRightName(),
						itemVo.getProperRightValue(), itemValueMap);
			} else {
				rightValue = new BigDecimal(0);
			}
			resultValue = CalculationMathValueUtil.calcuationValue(leftValue, rightValue, itemVo.getFormulaType());
			itemValueMap.put(itemVo.getItemName(), resultValue);
		}
		LumpValueMap.put(lumpVo.getLumpName(), resultValue);
		calculationResultVo.setResultValue(resultValue);
		calculationResultVo.setLeftValue(leftValue);
		calculationResultVo.setRightValue(rightValue);
	}

	public BigDecimal getProperValue(String properType, String properName, BigDecimal properValue,
			Map<String, BigDecimal> itemValueMap) {
		BigDecimal resultValue = new BigDecimal(0);
		if (ContentFinalUtil.PRO_TYPE_C.equals(properType)) {
			resultValue = properValue;
		} else if (ContentFinalUtil.PRO_TYPE_S.equals(properType)) {
			resultValue = itemValueMap.get(properName);
		} else if (ContentFinalUtil.PRO_TYPE_L.equals(properType)) {
			resultValue = LumpValueMap.get(properName);
		} else if (ContentFinalUtil.PRO_TYPE_P.equals(properType)) {
			if (propertiesMap.get(properName) != null) {
				try {
					resultValue = new BigDecimal(propertiesMap.get(properName).toString());
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}
		return resultValue;
	}

	public BigDecimal getProperValue(ItemPropertiesVo properVo, Map<String, BigDecimal> itemValueMap) {
		BigDecimal resultValue = new BigDecimal(0);
		if (ContentFinalUtil.PRO_TYPE_C.equals(properVo.getPropertiesType())) {
			resultValue = properVo.getBgValue();
		} else if (ContentFinalUtil.PRO_TYPE_S.equals(properVo.getPropertiesType())) {
			resultValue = itemValueMap.get(properVo.getPropertiesName());
		} else if (ContentFinalUtil.PRO_TYPE_L.equals(properVo.getPropertiesType())) {
			resultValue = LumpValueMap.get(properVo.getPropertiesName());
		} else if (ContentFinalUtil.PRO_TYPE_P.equals(properVo.getPropertiesType())) {
			resultValue = new BigDecimal(propertiesMap.get(properVo.getPropertiesName()).toString());
		}
		return resultValue;
	}

	public CalculationFormulaVo getCalculationFormulaVo() {
		return calculationFormulaVo;
	}

	public void setCalculationFormulaVo(CalculationFormulaVo calculationFormulaVo) {
		this.calculationFormulaVo = calculationFormulaVo;
	}

	public Map<String, Object> getPropertiesMap() {
		return propertiesMap;
	}

	public void setPropertiesMap(Map<String, Object> propertiesMap) {
		this.propertiesMap = propertiesMap;
	}

	public Map<String, BigDecimal> getLumpValueMap() {
		return LumpValueMap;
	}

	public void setLumpValueMap(Map<String, BigDecimal> lumpValueMap) {
		LumpValueMap = lumpValueMap;
	}

	public CalculationResultVo getCalculationResultVo() {
		return calculationResultVo;
	}

	public void setCalculationResultVo(CalculationResultVo calculationResultVo) {
		this.calculationResultVo = calculationResultVo;
	}

}
